package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.BabyAllResponse;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.widget.CuteButton;
import com.innovation.chengzhangriji.ui.widget.CuteTextView;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class SuperAdminActivity extends BaseActivity implements CuteToolbar.OnClickCuteToolbarListener, View.OnClickListener {

    private boolean delete;

    private String info;

    private DisplayImageOptions options;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1001 && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        } else if (requestCode == 1002 && resultCode == RESULT_OK) {
            delete();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_super_admin);
        CuteToolbar toolbar = (CuteToolbar) findViewById(R.id.toolbar);
        RoundedImageView imageHead = (RoundedImageView) findViewById(R.id.image_head);
        CuteTextView textName = (CuteTextView) findViewById(R.id.text_name);
        CuteTextView textPhone = (CuteTextView) findViewById(R.id.text_phone);
        CuteTextView textDate = (CuteTextView) findViewById(R.id.text_date);
        CuteButton btnCancel = (CuteButton) findViewById(R.id.btn_cancel);
        toolbar.setOnClickCuteToolbarListener(this);
        btnCancel.setOnClickListener(this);
        // 更新UI

        String permission = getIntent().getStringExtra("permission");
        BabyAllResponse babyAllResponse = get(permission, BabyAllResponse.class);
        if (babyAllResponse == null) {
            return;
        }
        toolbar.setCuteTitle(babyAllResponse.getName());
        textName.setText(babyAllResponse.getName());
        textPhone.setText(babyAllResponse.getPhone());
        String type = null;
        switch (babyAllResponse.getType()) {
            case 1:
                type = "创建日记";
                break;
            case 2:
                type = "由" + babyAllResponse.getUser() + "邀请加入";
                break;
            case 3:
                type = "由" + babyAllResponse.getUser() + "批准加入";
                break;
        }

        textDate.setText(babyAllResponse.getServerTS() + '\0' + type);

        int length = getIntent().getIntExtra("length", 1);
        if (length == 1) {
            delete = true;
            btnCancel.setText("删除日记本");
        }
        info = getIntent().getStringExtra("info");

        setOptions();
        ImageLoader.getInstance().displayImage(babyAllResponse.getPath(), imageHead, options);
    }

    @Override
    public void onClickLeft() {
        finish();
    }

    @Override
    public void onClickRight() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                if (delete) {
                    Intent intent = new Intent(this, DeleteDialogActivity.class);
                    startActivityForResult(intent, 1002);
                } else {
                    Intent intent = new Intent(this, ChangePermissionActivity.class);
                    intent.putExtra("info", info);
                    startActivityForResult(intent, 1001);
                }
                break;
        }
    }

    private void delete() {
        // 网络请求删除
        final LoginInfo loginInfo = Config.getLoginInfo(this);
        if (loginInfo == null) {
            return;
        }
        String phone = loginInfo.getPhone();
        String token = loginInfo.getToken().getAccessToken();
        int babyID = loginInfo.getBabyID();
        Client.requestRemoveNote(this, phone, token, babyID, new ResponseHandler() {

            @Override
            public void onInnovationStart() {
                showProgressDialog(null, null);
            }

            @Override
            public void onInnovationSuccess(JsonElement value) {
                // 删除日记信息

                dismissProgressDialog();
                sendDeleteBroadcast();
                setResult(-1000);
                finish();
            }

            @Override
            public void onChZhRJFailure(String value) {
                dismissProgressDialog();
                tipShow(value);
            }

            @Override
            public void onChZhRJTokenFailure(String value) {
                dismissProgressDialog();
                tipShow(value);
            }

        }, networkUnavailableSampleImpl());
    }

    private void sendDeleteBroadcast() {
        Intent intent = new Intent("delete");
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    private void setOptions() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_head_circle_default_small)    //在ImageView加载过程中显示图片
                .showImageForEmptyUri(R.drawable.ic_head_circle_default_small)  //image连接地址为空时
                .showImageOnFail(R.drawable.ic_head_circle_default_small)  //image加载失败
                .cacheOnDisk(true)   //加载图片时会在磁盘中加载缓存
                .build();
    }

}
